import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import fire_component_msg from '@feidao/web/atom/browser/fsm/fire-component-msg';

/**
 * 保存
 */
import nodejs from '@feidao/web/atom/browser/msg/nodejs';

export default async function (fd: IFeidaoAiBrowserComponent, ...args: any[]) {

	const form_title = document.querySelector<HTMLSpanElement>('[data-feidao-id="form_title"]');

	const data = args[0][0];
	const exam_id = fd.data.exam_id;
	const exam_name = fd.data.exam_name;

	const teacher_no = document.querySelector<HTMLInputElement>('[data-feidao-form="teacher_no"]').value;
	const teacher_name = document.querySelector<HTMLInputElement>('[data-feidao-form="teacher_name"]').value;
	const discipline = document.querySelector<HTMLInputElement>('[data-feidao-form="discipline"]').value;
	const bstype = document.querySelector<HTMLInputElement>('[data-feidao-form="bstype"]').value;
	const invigilation_num = document.querySelector<HTMLInputElement>('[data-feidao-form="invigilation_num"]').value;

	const teacher_id = fd.data.teacher_id;

	if (!exam_name) {
		alert('---- 没有获取到考试名称 -----');
		return;
	}
	if (!teacher_no) {
		alert('---- 请输入老师编号 -----');
		return;
	}
	if (!teacher_name) {
		alert('---- no teacher_name -----');
		return;
	}
	if (!bstype) {
		alert('---- no bstype -----');
		return;
	}
	if (!invigilation_num) {
		alert('---- no invigilation_num -----');
		return;
	}
	if (!discipline) {
		alert('---- no discipline -----');
		return;
	}

	const res = await (async () => {
		if (teacher_id) {
			const query = {
				rowdata: {
					_id: teacher_id, exam_id, teacher_no, teacher_name, exam_name, invigilation_num, bstype, discipline
				}
			};
			return await nodejs('flippedclass', 'usable-exam-teacher-upd', query);
		} else {
			const query = {
				rowdata: {
					exam_id, teacher_no, teacher_name, exam_name, invigilation_num, bstype, discipline
				}
			};
			return await nodejs('flippedclass', 'usable-exam-teacher-add', query);
		}
	})();

	if (res === 'success') {
		console.log('------- success -------');
		const edit = document.querySelector<HTMLDivElement>('[data-feidao-id="zj-teacher-002"]');
		edit.classList.add('display-n');
		fire_component_msg('zj-teacher-001', 'ec-002');
	} else if (res === 'fail:repeat') {
		alert('教师标号重复');
	} else {
		alert('调用服务失败');
	}
}